package Classic150;

import Tree.TreeNode;

public class 二叉树展开为链表 {
    /*
        一次操作后状态转为操作前相同状态，从而不断迭代处理
     */
    public void flatten(TreeNode root) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.left != null) {
                TreeNode connect = cur.left;
                while (connect.right != null) connect = connect.right;
                connect.right = cur.right;
                cur.right = cur.left;
                cur.left = null;
            }
            cur = cur.right;
        }
    }
}
